﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Statist
{
    public class StatisticsAppService : ApplicationService, IStatitsAppService
    {
        private readonly IRepository<Statistics, int> _StatisticsRepository;
        private readonly IRepository<MaterialType, int> _materialTypes;
        private readonly LogisticsDbContext _logisticsDbContext;

        public StatisticsAppService(IRepository<Statistics, int> StatisticsRepository, IRepository<MaterialType, int> materialTypes, LogisticsDbContext logisticsDbContext)
        {
            _logisticsDbContext = logisticsDbContext;
            _StatisticsRepository = StatisticsRepository;
            _materialTypes = materialTypes;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Procurement"></param>
        /// <returns></returns>
        public async Task<StatistDto> CreateAsync(StatistDto Procurement)
        {
            var Statistics = new Statistics
            {
                Name = Procurement.Name,
                Amount = Procurement.Amount,
                Balance = Procurement.Balance,
                Material=Procurement.Material,
                MaterialTypeId=Procurement.MaterialTypeId,
                Origin=Procurement.Origin,
                Quantity=Procurement.Quantity,
                Specifications=Procurement.Specifications,
            };

            await _StatisticsRepository.InsertAsync(Statistics);
            return ObjectMapper.Map<Statistics, StatistDto>(Statistics);
        }

        [RemoteService(false)]
        public async Task DeleteAsync(int id)
        {
            await _StatisticsRepository.DeleteAsync(id);
        }

        public async Task<StatistDto> GetAsync(int id)
        {
            var food = await _StatisticsRepository.GetAsync(id);
            return ObjectMapper.Map<Statistics, StatistDto>(food);
        }

        public async Task<PagedResultDto<StatistDto>> GetListAsync(GetStatitsListDto dto)
        {
            //var all = _ProcurementRepository.ToList();
            var all = _logisticsDbContext.Statistics.ToList();
            var type = _logisticsDbContext.MaterialType.ToList();
            var StatisticsType = from a in all
                           join c in type on a.MaterialTypeId equals c.Id
                           select new Statistics
                           {
                               Id = a.Id,
                               Name = a.Name,
                               Material=a.Material,
                               Amount=a.Amount,
                               Balance=a.Balance,
                               MaterialTypeId=a.MaterialTypeId,
                               Origin=a.Origin,
                               Quantity=a.Quantity,
                               Specifications=a.Specifications,
                               tname=c.Name
                           };
            int totoCount = StatisticsType.Count();
            var data = new PagedResultDto<StatistDto>();
            data.TotalCount = totoCount;
            StatisticsType = StatisticsType.Skip((dto.page - 1) * dto.limit).Take(dto.limit).ToList();
            data.Items = ObjectMapper.Map<List<Statistics>, List<StatistDto>>(StatisticsType.ToList());
            return data;
        }

        public int UpdateAsync(StatistDto Statist)
        {
            var Statistics = new Statistics
            {
                Name = Statist.Name,
                Amount = Statist.Amount,
                Material= Statist.Material,
                Balance= Statist.Balance,
                MaterialTypeId= Statist.MaterialTypeId,
                Origin = Statist.Origin,
                Quantity= Statist.Quantity,
                Specifications= Statist.Specifications,
                Id= Statist.Id
            };
            _logisticsDbContext.Entry(Statistics).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            var i = _logisticsDbContext.SaveChanges();
            return i;
        }
    }
}
